package com.usian.behavior.lister;

import com.mysql.cj.util.StringUtils;
import com.usian.behavior.service.IApFollowBehaviorService;
import com.usian.common.constants.message.FollowBehaviorConstants;
import com.usian.common.util.JsonUtils;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author Fay
 * @date 2022/7/18&19:55
 */
@Component
@Slf4j
public class FollowBehaviorListener {
    @Autowired
    private IApFollowBehaviorService apFollowBehaviorService;

    @KafkaListener(topics = FollowBehaviorConstants.FOLLOW_BEHAVIOR_TOPIC)

    public void receiverMessage(ConsumerRecord<?,?> record){
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);

        log.info("FollowBehaviorListener 接收到record=={}",record);
        if (optional.isPresent()){
            Object value = record.value();
            FollowBehaviorDto followBehaviorDto = JsonUtils.toBean(value.toString(), FollowBehaviorDto.class);
            apFollowBehaviorService.saveFollowBehavior(followBehaviorDto);
        }
    }
}
